package com.android.fastride.view.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.android.fastride.Constant;
import com.android.fastride.R;
import com.android.fastride.contract.CompletedOrderListContract;
import com.android.fastride.model.db.entity.Order;
import com.android.fastride.present.CompletedOrderListPresent;
import com.chad.library.adapter.base.BaseViewHolder;
import com.peng.library.mantis.expand.list.MantisListFragment;
import com.peng.library.mantis.expand.list.link.ListConfig;
import com.peng.library.mantis.expand.refresh.link.RefreshConfig;
import com.peng.library.mantis.hint.HintConfig;
import com.peng.library.mantis.mvp.BindPresent;

import butterknife.ButterKnife;
import butterknife.Unbinder;

@BindPresent(CompletedOrderListPresent.class)
public class OrderListFragment extends MantisListFragment<CompletedOrderListContract.PRESENT,
        Order> implements CompletedOrderListContract.VIEW, Constant {

    // 标识是新订单，进行中，已完成的状态，主要为请求不同数据设置。
    private int mOrderStatus;

    Unbinder unbinder;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mOrderStatus = getArguments().getInt(Constant.ORDER_STATUS, ORDER_COMPLETED);
        }
    }

    public static OrderListFragment newInstance(int type) {
        OrderListFragment completedOrderFragment = new OrderListFragment();
        Bundle arg = new Bundle();
        arg.putInt(Constant.ORDER_STATUS, type);
        completedOrderFragment.setArguments(arg);
        return completedOrderFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState,
                             HintConfig.Builder hintConfigBuilder,
                             RefreshConfig.Builder refreshConfigBuilder,
                             ListConfig.Builder listConfigBuilder) {
        View view = inflater.inflate(R.layout.fragment_completed_order, container, false);
        unbinder = ButterKnife.bind(this, view);
        hintConfigBuilder.layoutEmptyId(R.layout.hint_empty);
        refreshConfigBuilder.needRefreshViewId(R.id.rv_message_list);
        refreshConfigBuilder.refreshEnable(true);
        refreshConfigBuilder.autoRefresh(true);
        refreshConfigBuilder.pullToRefresh(true);
        listConfigBuilder.recyclerViewId(R.id.rv_message_list);
        listConfigBuilder.canLoadMore(true);
        listConfigBuilder.hasDivider(false);
        initView();
        return view;
    }

    private void initView() {

    }

    @Override
    public void onRetry(int type, Object tag) {
        super.onRetry(type, tag);
        // 点击没有数据图片或重试按钮时触发
        getPresent().onRefreshCall();
        getViewDelegate().showProgressView();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (unbinder != null) {
            unbinder.unbind();
            unbinder = null;
        }
    }

    @Override
    public void onConvert(BaseViewHolder holder, Order order, int position) {
        // 列表每个item的布局显示
        Button btn_get_order = holder.getView(R.id.btn_get_order);
        Button btn_call_seller = holder.getView(R.id.btn_call_seller);
        Button btn_call_customer = holder.getView(R.id.btn_call_customer);
        Button btn_pick_up = holder.getView(R.id.btn_pick_up);
        Button btn_complete = holder.getView(R.id.btn_complete);

        if (mOrderStatus == ORDER_PENDING) {
            btn_get_order.setVisibility(View.VISIBLE);
            btn_call_seller.setVisibility(View.GONE);
            btn_call_customer.setVisibility(View.GONE);
            btn_pick_up.setVisibility(View.GONE);
            btn_complete.setVisibility(View.GONE);
        } else {
            btn_get_order.setVisibility(View.GONE);
            btn_call_seller.setVisibility(View.VISIBLE);
            btn_call_customer.setVisibility(View.VISIBLE);
            btn_pick_up.setVisibility(mOrderStatus == ORDER_PICK_UP ? View.VISIBLE : View.GONE);
            btn_complete.setVisibility(mOrderStatus == ORDER_DISPATCH ? View.VISIBLE : View.GONE);
        }

        btn_get_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "抢单 is clicked", Toast.LENGTH_LONG).show();
            }
        });
        btn_call_seller.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                call("18611627529");
            }
        });
        btn_call_customer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                call("18611627529");
            }
        });
        btn_pick_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "待取货 is clicked", Toast.LENGTH_LONG).show();
            }
        });
        btn_complete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "已送达 is clicked", Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    public void onReceiveError(Throwable throwable) {

    }

    private void call(String phoneNum) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        Uri uri = Uri.parse("tel:" + phoneNum);
        intent.setData(uri);
        startActivity(intent);
    }

    @Override
    public int getItemLayout() {
        return R.layout.list_item_receive_of_delivery;
    }

    @Override
    public int getOrderStatus() {
        return mOrderStatus;
    }
}
